package com.jt.controller;

import com.jt.dao.RoleMapper;
import com.jt.entity.Role;
import com.jt.entity.Teacher;
import com.jt.service.RoleService;
import com.jt.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    private  TeacherService  teacherService;

    @Autowired
    private  RoleService     roleService;

    @RequestMapping("/queryAll")
    public  String  queryAll(ModelMap  modelMap){
        List<Teacher> teachers = teacherService.queryAll();
        modelMap.addAttribute("teachers",teachers);
        return  "/teacher/teacher_list";
    }

    @RequestMapping("/queryById")
    public  String   queryById(Integer id,ModelMap modelMap){
        Teacher teacher = teacherService.queryById(id);
        List<Role> roles = roleService.queryAll();
        modelMap.addAttribute("roles",roles);
        modelMap.addAttribute("teacher",teacher);
        return "/teacher/teacher_update";
    }

    @RequestMapping("/queryAllRole")
    public  String  queryAllRole(ModelMap modelMap){
        List<Role> roles = roleService.queryAll();
        modelMap.addAttribute("roles",roles);
        return  "/teacher/teacher_add";
    }

    @RequestMapping("/update")
    public  String   update(Integer id,String tname,Integer rid,Integer tsex,String tphone,String tcard,String twx){
        Teacher teacher = new Teacher();
        teacher.setTid(id);
        teacher.setTname(tname);
        if(tsex==1){
            teacher.setTsex("男");
        }else if(tsex==2){
            teacher.setTsex("女");
        }
        teacher.setTwx(twx);
        teacher.setTphone(tphone);
        teacher.setTcard(tcard);
        Role role = new Role();
        role.setId(rid);
        teacher.setRole(role);
        return "/teacher/teacher_add";
    }

    @RequestMapping("/save")
    public  String  save(String tname,Integer rid,Integer tsex,String tphone,String tcard,String twx){
        Teacher teacher = new Teacher();
        teacher.setTname(tname);
        if(tsex==1){
            teacher.setTsex("男");
        }else if(tsex==2){
            teacher.setTsex("女");
        }
        teacher.setTwx(twx);
        teacher.setTphone(tphone);
        teacher.setTcard(tcard);
        Role role = new Role();
        role.setId(rid);
        teacher.setRole(role);
        teacherService.save(teacher);
        return "/teacher/teacher_add";
    };

    @RequestMapping("/updateStatus")
    public  String  updateStatus(Integer id,Integer status){
        teacherService.updateStatus(id,status);
        return "forward:/teacher/queryAll";
    }

}
